package com.krtech.wecard.module.sys.service.impl;

import com.krtech.wecard.framework.model.CommRes;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.sys.entity.SysDept;
import com.krtech.wecard.module.sys.entity.SysUser;
import com.krtech.wecard.module.sys.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import com.krtech.wecard.module.sys.mapper.SysSchoolInfoMapper;
import com.krtech.wecard.module.sys.entity.SysSchoolInfo;
import com.krtech.wecard.module.sys.service.SysSchoolInfoService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysSchoolInfoServiceImpl extends ServiceImpl<SysSchoolInfoMapper, SysSchoolInfo> implements SysSchoolInfoService {

    @Autowired
    private SysDeptService deptService;


    @Override
    public int updateBatch(List<SysSchoolInfo> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<SysSchoolInfo> list) {
        return baseMapper.batchInsert(list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CommRes add(SysSchoolInfo sysSchoolInfo) {
        SysUser userInfo = LoginUtil.getUserInfo();

        sysSchoolInfo.setCreatedBy(userInfo.getUserId().toString());
        sysSchoolInfo.setCreatedTime(new Date());

        this.save(sysSchoolInfo);

        // 增加了学校同时要增加部门
        SysDept sysDept = new SysDept();
        sysDept.setDeptName(sysSchoolInfo.getName());
        sysDept.setAddress(sysSchoolInfo.getAddress());
        sysDept.setPid(0L);
        sysDept.setStatus(true);
        sysDept.setSort(1);
        sysDept.setCode(sysSchoolInfo.getSchoolCode());
        sysDept.setFullCode(sysSchoolInfo.getSchoolCode());
        sysDept.setFkSchoolCode(sysSchoolInfo.getId().toString());

        deptService.save(sysDept);

        return CommRes.ok(sysSchoolInfo);
    }
}




